package com.weipeep.sys.controller;


import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.MottoDO;
import com.weipeep.sys.service.MottoService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-12-19 14:58:39 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/motto")
public class MottoController {
	@Autowired
	private MottoService mottoService;
	
	@GetMapping()
	@RequiresPermissions("sys:motto:motto")
	String Motto(){
	    return "sys/motto/motto";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:motto:motto")
	public Result<Page<MottoDO>> list(Integer pageNumber, Integer pageSize, MottoDO mottoDTO){
		// 查询列表数据
        Page<MottoDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<MottoDO> wrapper = new EntityWrapper<MottoDO>(mottoDTO);
        page = mottoService.selectPage(page, wrapper);
        int total = mottoService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sys:motto:add")
	String add(){
	    return "sys/motto/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:motto:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MottoDO motto = mottoService.selectById(id);
		model.addAttribute("motto", motto);
	    return "sys/motto/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:motto:add")
	public Result<String> save( MottoDO motto){
		if(mottoService.insert(motto)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:motto:edit")
	public Result<String>  update( MottoDO motto){
		mottoService.updateById(motto);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:motto:remove")
	public Result<String>  remove( Long id){
		if(mottoService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:motto:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		mottoService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
